package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.antscore.annotation.PropertyObserve;
import com.qire.common.basic.StringUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.dal.service.TeensModeService;

import androidx.lifecycle.MutableLiveData;

/**
 * 青少年模式开启或关闭切换
 */
public class TeensModeToggleViewModel extends BaseViewModel implements IViewModel {

    private static final int EnablePanelOneID = 1;
    private static final int EnablePanelTwoID = 2;
    private static final int DisablePanelID = 3;

    private final PanelSate EnablePanelOne = new PanelSate(EnablePanelOneID, "设置密码", View.VISIBLE, "下一步", View.GONE);
    private final PanelSate EnablePanelTwo = new PanelSate(EnablePanelTwoID, "确认密码", View.GONE, "开启青少年模式", View.GONE);
    private final PanelSate DisablePanel = new PanelSate(DisablePanelID, "输入密码", View.GONE, "关闭青少年模式", View.VISIBLE);

    private final TeensModeService toggleService = new TeensModeService();

    private PanelSate panelState = EnablePanelOne;

    private String passwordOne;
    private String passwordTwo;

    @Override
    public int getVariableId() {
        return BR.teensModeToggleViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition() {
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
    }
    //endregion #附加管理 end


    // region    #业务

    public void nextAction() {
        switch (panelState.panelId) {
            case EnablePanelOneID : {
                passwordOne = panelState.passwordCode();
                showEnablePanelTwo();
                return;
            }
            case EnablePanelTwoID : {
                passwordTwo = panelState.passwordCode();
                if(passwordTwo.equals(passwordOne)) {
                    toggleService.enableTeensMode(passwordOne);
                } else {
                    ToastHelper.showSnackBar("输入密码不一致", Prompt.ERROR);
                }
                return;
            }
            case DisablePanelID : {
                passwordOne = panelState.passwordCode();
                toggleService.disableTeensMode(passwordOne);
                return;
            }
        }
    }

    public boolean canBack() {
        switch (panelState.panelId) {
            case EnablePanelTwoID:
                showEnablePanelOne();
                return true;
            case EnablePanelOneID:
            case DisablePanelID:
            default:
                return false;
        }
    }

    // endregion #业务

    // region    #UI操作

    public void showEnablePanelOne() {
        panelState = EnablePanelOne;
        resetPasswordCodeUI();
        refreshUI();
    }
    public void showEnablePanelTwo() {
        panelState = EnablePanelTwo;
        resetPasswordCodeUI();
        refreshUI();
    }
    public void showDisablePanel() {
        panelState = DisablePanel;
        resetPasswordCodeUI();
        refreshUI();
    }

    private void refreshUI() {
        set("title", panelState.title);
        set("passwordTipsVisible", panelState.passwordTipsVisible);
        set("nextBtnText", panelState.nextBtnText);
        set("forgetPasswordTipsVisible", panelState.forgetPasswordTipsVisible);
        refreshNextBtnBgRid(panelState.isFull());
    }

    private void refreshNextBtnBgRid(boolean clickable) {
        set("nextBtnClickable", clickable);
        set("nextBtnBgRid", clickable ? R.drawable.app_teens_button_yellow : R.drawable.app_teens_button_gray);
    }

    private void resetPasswordCodeUI() {
        set("passwordCode", new CharSequence[] {"", "", "", ""});
        set("codeFocusIndex", 0);
    }

    @PropertyObserve(name = "passwordCode")
    public void onPasswordCodeChange(CharSequence[] passwordCode) {
        ArrayUtil.replace(passwordCode, panelState.passwordCodes);
        refreshNextBtnBgRid(panelState.isFull());
    }

    // endregion #UI操作

    // region    #UI绑定
    public MutableLiveData<CharSequence> getTitle() {
        return get("title");
    }

    public MutableLiveData<Integer> getPasswordTipsVisible() {
        return get("passwordTipsVisible");
    }

    public MutableLiveData<CharSequence[]> getPasswordCode() {
        return get("passwordCode");
    }

    public MutableLiveData<Integer> getCodeFocusIndex() {
        return get("codeFocusIndex");
    }

    public MutableLiveData<Boolean> getNextBtnClickable() {
        return get("nextBtnClickable");
    }

    public MutableLiveData<CharSequence> getNextBtnText() {
        return get("nextBtnText");
    }

    public MutableLiveData<Integer> getNextBtnBgRid() {
        return get("nextBtnBgRid");
    }

    public MutableLiveData<Integer> getForgetPasswordTipsVisible() {
        return get("forgetPasswordTipsVisible");
    }
    // endregion #UI绑定

    private class PanelSate {

        final int panelId;
        final String title;
        final int passwordTipsVisible;
        final String nextBtnText;
        final int forgetPasswordTipsVisible;

        final String[] passwordCodes = new String[4];

        PanelSate(int panelId, String title, int passwordTipsVisible, String nextBtnText, int forgetPasswordTipsVisible) {
            this.panelId = panelId;
            this.title = title;
            this.passwordTipsVisible = passwordTipsVisible;
            this.nextBtnText = nextBtnText;
            this.forgetPasswordTipsVisible = forgetPasswordTipsVisible;
        }

        public boolean isFull() {
            for(String code : passwordCodes) {
                if(StringUtil.isEmpty(code)) {
                    return false;
                }
            }
            return true;
        }

        public String passwordCode() {
            StringBuilder passwordCodeBuilder = new StringBuilder(4);
            for(String code : passwordCodes) {
                if(StringUtil.notEmpty(code)) {
                    passwordCodeBuilder.append(code.charAt(0));
                }
            }
            return passwordCodeBuilder.toString();
        }

    }
}
